package geektime.spring.springbucks;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import geektime.spring.springbucks.model.Coffee;
import geektime.spring.springbucks.model.CoffeeExample;
import geektime.spring.springbucks.service.CoffeeService;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@EnableTransactionManagement
@SpringBootApplication
@EnableCaching

@MapperScan("geektime.spring.springbucks.mapper")
public class SpringBucksApplication implements ApplicationRunner {
//	@Autowired
//	private CoffeeRepository coffeeRepository;
	@Autowired
	private CoffeeService coffeeService;
	
	public static void main(String[] args) {
		SpringApplication.run(SpringBucksApplication.class, args);
	}

	@Override
	public void run(ApplicationArguments args) throws Exception {
		addSomeNewCoffee();
		addAndDeleteCoffee();
		searchCoffeePagging();
		batchSearchCoffeeByIdBatch();
	}
	
	//新增一些种类的咖啡
	private void addSomeNewCoffee() {
		//应用启动后，默认插入espresso
		List<Coffee> allCoffee = coffeeService.findAllCoffee(null);
		System.out.println(allCoffee);
		
		//插入其他类型咖啡
		Coffee cfe = new Coffee();
		coffeeService.addCoffee(cfe.withName("latte")
				.withPrice(Money.of(CurrencyUnit.of("CNY"), 25.0))
				.withCreateTime(new Date())
				.withUpdateTime(new Date()));
		coffeeService.addCoffee(cfe.withName("capuccino")
				.withPrice(Money.of(CurrencyUnit.of("CNY"), 26.0))
				.withCreateTime(new Date())
				.withUpdateTime(new Date()));
		coffeeService.addCoffee(cfe.withName("mocha")
				.withPrice(Money.of(CurrencyUnit.of("CNY"), 27.0))
				.withCreateTime(new Date())
				.withUpdateTime(new Date()));
		coffeeService.addCoffee(cfe.withName("macchiato")
				.withPrice(Money.of(CurrencyUnit.of("CNY"), 28.0))
				.withCreateTime(new Date())
				.withUpdateTime(new Date()));
		List<Coffee> allCoffee_new = coffeeService.findAllCoffee(null);
		//打印所有咖啡
		System.out.println(allCoffee_new);
	}
	
	//新增一款咖啡，然后删除它
	private void addAndDeleteCoffee() {
		Coffee cfe = new Coffee();
		coffeeService.addCoffee(cfe.withName("超贵咖啡")
				.withPrice(Money.of(CurrencyUnit.of("CNY"), 120.0))
				.withCreateTime(new Date())
				.withUpdateTime(new Date()));
		CoffeeExample example = new CoffeeExample();
		example.createCriteria().andNameEqualTo("超贵咖啡");
		List<Coffee> allCoffee = coffeeService.findAllCoffee(example);
		System.out.println("成功加入了===="+allCoffee.get(0).toString());
		//太贵了，卖的不好删除它
		coffeeService.deleteCoffee(example);
		allCoffee = coffeeService.findAllCoffee(example);
		System.out.println("没有了===="+allCoffee.size());
	}
	
	//分页查询coffee
	private void searchCoffeePagging() {
		coffeeService.findAllWithRowBounds(null,new RowBounds(1, 3))
		.forEach(c -> log.info("第一页：{}", c));
		coffeeService.findAllWithRowBounds(null,new RowBounds(2, 3))
				.forEach(c -> log.info("第二页：{}", c));
		//从缓冲中抽出第二页的数据
		List<Coffee> cacheList = coffeeService.findFromCache();
		cacheList.forEach(c -> log.info("这是缓存数据：{}",c.toString()));
		try {
			Thread.sleep(6000);
			log.info("缓存超时，数据没了：{}",coffeeService.findFromCache());
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	//根据ID批量查询
	private void batchSearchCoffeeByIdBatch() {
		CoffeeExample example = new CoffeeExample();
		example.createCriteria().andIdIn(Arrays.asList(new Long[] {1L,2L,3L,4L,5L}));
		List<Coffee> list = coffeeService.findAllCoffee(example);
		list.forEach(c -> log.info(c.toString()));
	}
	
}

